package com.yjkf.zh.admin.controller;

import com.yjkf.zh.admin.aspect.LogAnnotation;
import com.yjkf.zh.admin.dao.SysCollectMapper;
import com.yjkf.zh.admin.model.SysCollect;
import com.yjkf.zh.admin.service.SysCollectService;
import com.yjkf.zh.core.http.HttpResult;
import com.yjkf.zh.core.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("collect")
public class SysCollectController {

    @Autowired
    private SysCollectService sysCollectService;

    @Autowired
    private SysCollectMapper sysCollectMapper;

    @PreAuthorize("hasAuthority('sys:collect:view')")
    @PostMapping("/findPage")
    public HttpResult findPage(@RequestBody PageRequest pageRequest, HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("userId");
        return HttpResult.ok((sysCollectService.findCollectName(pageRequest,userId)));
    }

    @PreAuthorize("hasAuthority('sys:collect:add') AND hasAuthority('sys:collect:edit')")
    @PostMapping(value = "/save")
    @LogAnnotation(content = "添加收藏组")
    public HttpResult save(@RequestBody SysCollect sysCollect,HttpServletRequest request){
        Long createId = (Long) request.getSession().getAttribute("userId");
        return HttpResult.ok(sysCollectService.save(sysCollect,createId));
    }

    @GetMapping("/selectCollectName")
    public HttpResult selectColectName(HttpServletRequest request){
        Long createId = (Long) request.getSession().getAttribute("userId");
        return HttpResult.ok(sysCollectMapper.selectCollectName(createId));
    }

    @GetMapping("/collectTime")
    public HttpResult collectTime(@RequestParam Long id){
        return HttpResult.ok(sysCollectMapper.collectTime(id));
    }

    @PostMapping(value = "/delete")
    @LogAnnotation(content = "删除收藏组")
    public HttpResult delete(@RequestParam Long id){
        return HttpResult.ok(sysCollectService.delete(id));
    }
}
